<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Criptografar texto</title>
    </head>
    <body>
        <form action="index.php" method="post">
            <input type="text" name="texto_cript" id="cript" />&nbsp;
            <input type="submit" name="cript" value="Criptografar"/><br>
            <input type="text" name="texto_decript" id="decript"/>&nbsp;
            <input type="submit" name="decript" value="Decriptografar"/>
        </form>
        <?php
        
            //Declração de variáveis
            $array_cript = array();
  
            montaArray();
            
            if(isset($_POST['cript']))
            {
                $txt_cript = $_POST['texto_cript'];
                criptografar($array_cript,$txt_cript);    
            }
            else if(isset($_POST['decript']))
            {
                $txt_decript = $_POST['texto_decript'];
                decriptografar($array_cript,$txt_decript);
            }
            
            function montaArray() {
                
                global $array_cript;
                
                $array_cript['a'] = "01100001";
                $array_cript['b'] = "01100010";
                $array_cript['c'] = "01100011";
                $array_cript['d'] = "01100100";
                $array_cript['e'] = "01100101";
                $array_cript['f'] = "01100110";
                $array_cript['g'] = "00100111";
                $array_cript['h'] = "01101000";
                $array_cript['i'] = "01101001";
                $array_cript['j'] = "01101010";
                $array_cript['k'] = "01101011";
                $array_cript['l'] = "01101100";
                $array_cript['m'] = "01101101";
                $array_cript['n'] = "01101110";
                $array_cript['o'] = "01101111";
                $array_cript['p'] = "01110000";
                $array_cript['q'] = "01110001";
                $array_cript['r'] = "01110010";
                $array_cript['s'] = "01110011";
                $array_cript['t'] = "01110100";
                $array_cript['u'] = "01110101";
                $array_cript['v'] = "01110110";
                $array_cript['w'] = "01110111";
                $array_cript['x'] = "01111000";
                $array_cript['y'] = "01111001";
                $array_cript['z'] = "01111010"; 
            }
            
            function criptografar($array,$texto) {

                $qnt = strlen($texto);
                $cript = "";
                if($qnt != 0)
                {
                   for ($i = 0; $i < $qnt; $i++) {
                    
                    $cript.= $array[$texto[$i]];   
                    
                   }
                   
                   echo "<script> document.getElementById('decript').value ='$cript';</script>";
                }
                else
                {
                    exit;
                }
   
            }
            
            function decriptografar($array,$texto) {
                
                $qnt = strlen($texto);
                $i = 0;
                $key = "";
                
                while($i<$qnt){
                     
                    $cript = substr($texto, $i, $i+8);
                    
                    $key .= array_search($cript, $array);
                    
                    if($i >8){
                        $i = $i+8;
                        $cript = NULL;
                    }
                    else
                    {
                        $i = $i+9;
                        $cript = NULL;
                    }
                }
                
                echo "<script> document.getElementById('cript').value ='$key';</script>";
                
            }
        ?>
    </body>
</html>
